package com.oasistudio.ads.expert.banner;

import net.youmi.android.AdView;
import net.youmi.android.AdViewListener;
import android.app.Activity;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

import com.oasistudio.ads.expert.R;

/**
 * 有米的AdView类是final的，不能继承，且没有提供广告请求的调用接口。
 */
public class AdViewYoumi {
	YoumiAdRequestListener adRequestListener;
	public static final String UNION = "youmi.net";
	Activity activity;
	public AdViewYoumi(Activity activity){
		this.activity = activity;
		this.adRequestListener = new YoumiAdRequestListener(activity, UNION);
		
		addYoumiAd();
	}
	
	/************************** www.youmi.net start *************************/
	private void addYoumiAd() {
		LinearLayout adContainer = (LinearLayout) activity.findViewById(R.id.ads_container);
		adContainer.addView(new TitleBuilder().createTitle(activity, UNION));
		net.youmi.android.AdManager.init(activity, "573543fd5d2240f5", "5d6472b2b67cb93f", 30, false);
		LinearLayout youmiContainer = new LinearLayout(activity);
		adContainer.addView(youmiContainer);
		// adContainer.setOrientation(LinearLayout.VERTICAL);
		// layout.setBackgroundResource(R.drawable.bg);
		net.youmi.android.AdView adView = new net.youmi.android.AdView(activity);
		adView.setAdViewListener(this.adRequestListener);
		
		LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
		youmiContainer.addView(adView, params);
	}
	/************************** www.youmi.net end *************************/
	
	class YoumiAdRequestListener extends AbstractAdsExpertAdListener implements AdViewListener{
		
		public YoumiAdRequestListener(Activity activity, String union){
			super(activity, union);
		}
		
		@Override
		public void onAdViewSwitchedAd(AdView adView) {
			// TODO Auto-generated method stub
			System.out.println(".............Youmi onAdViewSwitchedAd() AdView.clickable = "+adView.isClickable());
			this.succeed();
			AdHistory.saveAd(UNION, adView);
		}

		@Override
		public void onConnectFailed(AdView arg0) {
			// TODO Auto-generated method stub
			System.out.println("Youmi onConnectFailed() AdView.clickable = "+arg0.isClickable());
			this.fail();
		}
	}
}
